package com.lhkj.ct.admin.modules.psychology.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.lhkj.ct.base.annntation.Log;
import com.lhkj.ct.base.enums.ActionType;
import com.lhkj.ct.base.enums.DeleteStatus;
import com.lhkj.ct.base.model.dto.PageQuery;
import com.lhkj.ct.base.model.ro.ActionResult;
import com.lhkj.ct.base.utils.Validate;
import com.lhkj.ct.meta.modules.psychology.model.dto.ReportContentDto;
import com.lhkj.ct.meta.modules.psychology.model.entity.TblScaleReport;
import com.lhkj.ct.meta.modules.psychology.model.vo.EvaluationHistoryVo;
import com.lhkj.ct.meta.modules.psychology.model.vo.PersonalReportInfo;
import com.lhkj.ct.meta.modules.psychology.service.ScaleReportContentService;
import com.lhkj.ct.meta.modules.psychology.service.ScaleReportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * <p>
 *      测验报告
 * </p>
 *
 * @author jobob
 * @since 2023-10-16
 */
@RestController
@RequestMapping("api/plat/psychology/personal/report")
@Api(tags = {"2.3个体报告"})
@Validated
public class PsyReportController {

    @Resource
    private ScaleReportService reportService;

    @Resource
    private ScaleReportContentService reportContentService;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "报告列表")
    @GetMapping("list")
//    //@RequiresPermissions(value = {"patient:unit:all"}, logical = Logical.OR)
    public ActionResult<IPage<TblScaleReport>> selectPatUnitPage(TblScaleReport report, PageQuery pageQuery) {
        return ActionResult.success(reportService.selectReportPage(report, pageQuery));
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "用户测评历史")
    @GetMapping("{patId}/histories")
    public ActionResult<List<EvaluationHistoryVo>> selectHistoriesByUser(@PathVariable String patId) {
        return ActionResult.success(reportService.selectHistoriesByUser(patId));
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "查看报告")
    @GetMapping("{rId}/get")
//    //@RequiresPermissions(value = {"patient:unit:all"}, logical = Logical.OR)
    public ActionResult<PersonalReportInfo> selectPatUnitPage(@PathVariable String rId) {
        return ActionResult.success(reportService.getReportInfo(rId));
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "编辑报告接口")
    @PutMapping(value = "{rId}update")
    @Log(title = "编辑测验报告", actionType = ActionType.UPDATE)
//    //@RequiresPermissions(value = {"XLCY:GTBG:UPDATE"}, logical = Logical.OR)
    public ActionResult<Void> update(@PathVariable String rId,
                                     @Validated @RequestBody List<ReportContentDto> reportContent) {
        TblScaleReport report = reportService.getById(rId);
        Validate.isFalse(null == report || DeleteStatus.DELETED.equals(report.getDelFlag()),
                "报告不存在或已删除！");
        Validate.validState(report.getReportStatus() == 0,
                "报告已审核，不可编辑！");
        return ActionResult.judge(reportContentService.updateContent(reportContent));
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "审核报告", notes = "仅支持单个")
    @PutMapping(value = "{rId}/review")
    @Log(title = "审核测验报告", actionType = ActionType.UPDATE)
//    //@RequiresPermissions(value = {"XLCY:GTBG:REVIEW"}, logical = Logical.OR)
    public ActionResult<Void> review(@PathVariable String rId) {
        if (reportService.review(rId)) {
            return ActionResult.success("审核成功");
        }
        return ActionResult.fail("审核失败");
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "发布报告", notes = "仅支持单个")
    @PutMapping(value = "publish")
    @Log(title = "发布测验报告", actionType = ActionType.UPDATE)
//    //@RequiresPermissions(value = {"XLCY:GTBG:PUBLISH"}, logical = Logical.OR)
    public ActionResult<Void> publish(@NotEmpty @RequestBody List<String> ids) {
        int num = reportService.publish(ids);
        if (num == 0) {
            return ActionResult.fail("发布失败");
        }
        return ActionResult.success(String.format("发布成功,共发布%d条", num));
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "删除报告", notes = "支持批量删除")
    @DeleteMapping("delete")
    @Log(title = "删除测验报告", actionType = ActionType.DELETE)
    public ActionResult<Void> delete(@NotEmpty @RequestBody List<String> ids) {
        int count = reportService.removeByIds(ids);
        return ActionResult.success(String.format("删除完成，共删除[%d]条", count));
    }

    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "导出word", notes = "仅支持单个")
    @PutMapping(value = "{rId}/exportWord")
    @Log(title = "导出测验报告", actionType = ActionType.EXPORT, isSaveResponseData = false)
    public void exportWord(@PathVariable String rId) {
        reportService.exportWord(rId);
    }

    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "导出pdf", notes = "仅支持单个")
    @PutMapping(value = "{rId}/exportPdf")
    @Log(title = "导出测验报告", actionType = ActionType.EXPORT, isSaveResponseData = false)
    public void exportPdf(@PathVariable String rId) {
        reportService.exportPdf(rId);
    }
}
